package com.tengju.bff.interfaces.manage.club;

import com.tengju.bff.interfaces.manage.club.facade.ClubFacade;
import com.tengju.bff.interfaces.manage.club.vo.ClubVO;
import com.tengju.bff.interfaces.manage.club.vo.PageClubInfoVO;
import com.tengju.bff.interfaces.manage.club.vo.UserWithClubVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.user.application.club.ClubManageService;
import com.tengju.user.application.club.dto.*;
import com.tengju.user.domain.model.club.ClubException;
import com.tengju.user.domain.model.user.Staff;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 俱乐部管理
 * @author chenhang
 * @Date 2021/1/12
 */
@Validated
@RestController
@Api(tags = "俱乐部管理")
@RequestMapping(value = "manage/club")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ClubController {

    private final ClubManageService clubManageService;
    private final ClubFacade clubFacade;


    @RequiresRoles("sso")
    @ApiOperation(value = "俱乐部列表查询", notes = "俱乐部列表查询")
    @PostMapping(value = "listClub", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<PageClubInfoVO> listClubInfo(@RequestBody ClubQueryDTO clubQueryDTO) {
        PageClubInfoVO pageClubInfoVO = clubFacade.listClubInfo(clubQueryDTO);
        return ApiResponse.newSuccess(pageClubInfoVO);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "新增俱乐部", notes = "新增俱乐部")
    @PostMapping(value = "addClub", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> addClub(@RequestBody ClubAddDTO clubAddDTO) {
        Staff staff = ManageUtil.getOperateUser();
        clubManageService.add(clubAddDTO,staff);
        return ApiResponse.newSuccess(true);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "更新俱乐部信息", notes = "更新俱乐部信息")
    @PostMapping(value = "updateClub", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> updateClub(@RequestBody ClubUpdateDTO clubUpdateDTO) {
        Staff staff = ManageUtil.getOperateUser();
        clubManageService.updateClub(clubUpdateDTO,staff);
        return ApiResponse.newSuccess(true);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "添加俱乐部成员", notes = "添加俱乐部成员")
    @PostMapping(value = "addClubMember", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> addClubMember(@RequestBody ClubAddMemberDTO clubAddMemberDTO) {
        clubManageService.addClubMember(clubAddMemberDTO);
        return ApiResponse.newSuccess(true);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "查询俱乐部成员信息", notes = "查询俱乐部成员信息")
    @GetMapping(value = "getClubUserInfo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<UserWithClubVO> getClubUserInfo(@RequestParam Long idCode) {
        if (idCode == null) {
            throw new ClubException("用户idCode不能为空");
        }
        UserWithClubVO userWithClubVO = clubFacade.getClubUserInfo(idCode);
        return ApiResponse.newSuccess(userWithClubVO);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "用户更换俱乐部", notes = "用户更换俱乐部")
    @PostMapping(value = "userChangeClub", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> userChangeClub(@RequestBody UserChangeClubDTO userChangeClubDTO) {
        clubManageService.userChangeClub(userChangeClubDTO);
        return ApiResponse.newSuccess(true);
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "俱乐部查询", notes = "俱乐部查询")
    @PostMapping(value = "clubName", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<List<ClubVO>> getClubList() {
        return ApiResponse.newSuccess(clubFacade.getAllClub());
    }


}
